package net.bac.sbe.web.sonar.server.report.wrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.bac.sbe.critical.item.info.service.CriticalItemService;
import net.bac.sbe.svn.view.SbeSVNDiffItemView;
import net.bac.sbe.web.report.ReportFormat;
import net.bac.sbe.web.report.ReportGenerator;
import net.bac.sbe.web.report.itemstoretain.ItemToRetainReport;
import net.bac.sbe.web.sonar.client.view.ReportView;
import net.bac.sbe.web.sonar.server.SVNServiceWrapper;
import net.bac.sbe.web.sonar.server.dev.utils.xml.applications.ApplicationsConfig;

import com.google.inject.Inject;


public class ItemToRetainReportServiceWrapper {


	@Inject
	private CriticalItemService criticalItemService;

	@Inject
	private SVNServiceWrapper svnWrapper;

	@Inject
	@ItemToRetainReport
	private ReportGenerator reportGenerator;

	public void setReportGenerator(ReportGenerator reportGenerator) {
		this.reportGenerator = reportGenerator;
	}

	public void setCriticalItemService(CriticalItemService criticalItemService) {
		this.criticalItemService = criticalItemService;
	}

	public void setSvnWrapper(SVNServiceWrapper svnWrapper) {
		this.svnWrapper = svnWrapper;
	}

	public ReportView generateItemToRetainReport(String projectName,
		String projectAnalyst, String projectDescription, List<String> repositories, ServletContext context, String repToCompare) {

		Map<String, List<Object>> data = getItemToRetainReportData(projectName, repositories, repToCompare);

		ReportView reportView = getItemToRetainReportView(projectName);

		String reportAbsolutePath = context.getRealPath(reportView.getReportURLForDownload());


		HashMap<String, Object> reportParameters = new HashMap<String, Object>(7);

		reportParameters.put(ReportGenerator.PROJECT_NAME_KEY, getProjectTitleForReport(projectName, projectDescription));
		reportParameters.put(ReportGenerator.ANALYST_NAME_KEY, projectAnalyst);
		reportParameters.put(ReportGenerator.REPORT_DATA_SET_1, data);
		reportParameters.put(ReportGenerator.REPORT_FORMAT_KEY, ReportFormat.PDF);
		reportParameters.put(ReportGenerator.REPORT_ABSOLUTE_PATH_KEY, reportAbsolutePath);
		reportParameters.put(ReportGenerator.NEW_ITEM_QUANTITY_KEY, getNewItemCount(data));
		reportParameters.put(ReportGenerator.MODIFIED_ITEMS_QUANTITY_KEY, getModItemCount(data));


		reportGenerator.generateReport(reportParameters);

		return reportView;
	}

	private Map<String, List<Object>> getItemToRetainReportData(String projectName, List<String> repositories, String repToCompare) {

		Map<String, List<Object>> data = new HashMap<String, List<Object>>();

		List<SbeSVNDiffItemView> changes = svnWrapper.getModifiedItems(projectName, repositories, repToCompare);

		data.put("PROYECTO", new ArrayList<Object>(1));
		data.put("MODULO", new ArrayList<Object>(1));
		data.put("BIBLIOTECA", new ArrayList<Object>(1));

		data.put("ITEM", new ArrayList<Object>(1));
		data.put("TIPO", new ArrayList<Object>(1));
		data.put("ES_NUEVO", new ArrayList<Object>(1));
		data.put("ES_CRITICO", new ArrayList<Object>(1));

		for (SbeSVNDiffItemView sbeSVNDiffItemView : changes) {
			if (' ' != sbeSVNDiffItemView.getModificationCode()
					&& 'D' != sbeSVNDiffItemView.getModificationCode() && sbeSVNDiffItemView.isFile()) {
				String project = ApplicationsConfig.getInstance().getApplicationByRegularExpression(sbeSVNDiffItemView.getPath());
				if (null == project || "".equals(project)){
					project = ApplicationsConfig.getInstance().getApplicationByRegularExpression(sbeSVNDiffItemView.getSvnURL());
				}
				data.get("PROYECTO").add(project);
				String module = ApplicationsConfig.getInstance().getModuleByRegularExpression(sbeSVNDiffItemView.getPath());
				if (null == module || "".equals(module)){
					module = "/";
				}
				data.get("MODULO").add(module);

				data.get("TIPO").add(sbeSVNDiffItemView.getExtension());
				String folder = sbeSVNDiffItemView.getFolder();
				if (folder == null || "".equals(folder)){
					folder = "/";
				}
				data.get("BIBLIOTECA").add(folder);
				data.get("ITEM").add(sbeSVNDiffItemView.getItem());

				if (sbeSVNDiffItemView.getModificationCode() == 'A') {
					data.get("ES_NUEVO").add("si");
				} else {
					data.get("ES_NUEVO").add("no");
				}

				if (criticalItemService.isCriticalItem(sbeSVNDiffItemView.getItem())){
					data.get("ES_CRITICO").add("si");
				} else {
					data.get("ES_CRITICO").add("no");
				}
			}
		}

		return data;
	}

	private ReportView getItemToRetainReportView(String projectName) {
		ReportView reportView = new ReportView();
		reportView.setReportName("Hoja de Pase - ".concat(projectName)
				.concat(".pdf"));
		reportView
				.setReportContextualURL("reports/itemsToRetain");
		return reportView;
	}

	private String getProjectTitleForReport(String projectName,
			String projectDescription) {
		return projectName.concat(" - ").concat(projectDescription);
	}

	private int getNewItemCount(Map<String, List<Object>> data) {
		int count = 0;
		List<Object> itemsData = data.get("ES_NUEVO");
		for (Object object : itemsData) {
			if ("si".equalsIgnoreCase((String)object)){
				count++;
			}

		}
		return count;
	}

	private int getModItemCount(Map<String, List<Object>> data) {
		int count = 0;
		List<Object> itemsData = data.get("ES_NUEVO");
		for (Object object : itemsData) {
			if ("no".equalsIgnoreCase((String)object)){
				count++;
			}

		}
		return count;
	}

}
